<!--
  智慧小区管理系统 - 权限容器组件（仅支持菜单权限）
  @author Wu.Liang
  @date 2024-01-01
  @updated 2025-07-03 - 权限简化，仅保留菜单权限判断
-->
<template>
  <div v-if="hasMenuPermission" class="permission-container">
    <slot />
  </div>
  <div v-else-if="showFallback" class="permission-fallback">
    <slot name="fallback">
      <el-empty description="暂无权限访问此内容" />
    </slot>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useUserStore } from '@/store/user'
import { usePermissionStore } from '@/store/permission'
import { ROLE_CODES } from '@/constants/roles'
import { isSuperAdmin, hasPermission } from '@/utils/permission'

// Props
interface Props {
  permission: string // 仅支持菜单权限编码，如 user:menu
  showFallback?: boolean
  fallbackMessage?: string
}

const props = withDefaults(defineProps<Props>(), {
  permission: '',
  showFallback: false,
  fallbackMessage: '暂无权限访问此内容'
})

// Store
const userStore = useUserStore()
const permissionStore = usePermissionStore()

// 检查权限
const checkPermission = () => {
  // 使用统一的权限判断方法
  if (isSuperAdmin()) return true
  return hasPermission(props.permission)
}

// 仅菜单权限判断
const hasMenuPermission = computed(() => {
  if (!props.permission) return true
  return checkPermission()
})
</script>

<style scoped>
.permission-container {
  width: 100%;
}

.permission-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: #909399;
}
</style> 